package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.app.service.LoginService;

/**
 * 登录
 *  
 * @author twj
 * 
 */
@SuppressWarnings("serial")
public class LoginAction extends AppBaseAction {
	@Resource(name = LoginService.BEAN_NAME)
	LoginService loginService;

	public String login() {
		JSONObject jsonObject = null;
		try {
			String userName = this.request.getParameter("userName");
			String password = this.request.getParameter("password");
			String sessionId = this.request.getParameter("sessionId");//匿名变量，用于匿名购物车，登录后把匿名购物车里的商品转为个人商品
			this.validateSign(userName,password,sessionId);//验证签名是否正确 userName+ password+sessionId
			jsonObject = loginService.loginApp(userName, password,getSign(),"");
			
			String agent = request.getHeader("USER-AGENT");//
			String memo = "会员在手机上登录app."+agent;
			this.logService.addMallLog(jsonObject.getString("userId"), getBrowser(), getIp(), memo.length()>400?memo.substring(0, 400):memo);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 退出系统
	 * @return
	 */
	public String logout() {
		JSONObject jsonObject = null;
		try {
			String deviceId = this.request.getParameter("deviceId");
			this.validateSign(getUserId());//验证签名是否正确 userId 
			//this.validateLogin();//验证是否登录
			jsonObject = loginService.logoutApp(getUserId(),deviceId);
			this.logService.addMallLog(getUserId(), this.getBrowser(), this.getIp(), "会员在手机上退出app");
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

	/**
	 * 当前用户是否被踢
	 * @return
	 */
	public String isLogin(){
		JSONObject jsonObject = new JSONObject();
		try {
			boolean flag = this.isLoginApp();
			if (flag) {
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);	
				jsonObject.put(ErrorMsg.KEY_MESSAGE, "该用户在线");	
			} else {
				jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.put(ErrorMsg.KEY_MESSAGE, "该用户离线");	
			}
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 *  抢红包登录接口
	 *  @return
	 *
	 */
	public String loginForOther(){
		JSONObject jsonObject = null;
		
		try{
			
			String userName = this.request.getParameter("userName");
			String password = this.request.getParameter("password");
			this.validateSign(userName,password);//验证签名是否正确 userName+ password+sessionId
			jsonObject = this.loginService.loginApp(userName, password); //只验证用户名和密码是或否正确
			if(jsonObject != null){
			this.logService.addMallLog(jsonObject.getString("userId"), this.getBrowser(), this.getIp(), "抢红包登录");
			}
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
	    return this.writeJson(jsonObject);
	}
}
